Object.extend(Element.Methods, {
		stopObservingNested: function(element, flag) {
				element.descendants().invoke('stopObserving');

				if (flag)
				{
					element.stopObserving();
				}


				try
				{
					return element;
				}
				finally
				{
					element = null;
				}
		}
});

Element.addMethods(); 

var mask_index = [];

//如果滚动条存在，则longding居中，可能不在viewpoint中
Element.addMethods({

	mask: function(element){
		var id = element.identify();

		if (-1 != mask_index.indexOf(id)  && element != document.body)
		{
			return -1;
		}

		mask_index.push(id);

		var prefix = 'm_';
		var mask_id = prefix + id;

		if (element)
		{
			var elt = new Element("div",{id:mask_id, 'class':"waiting_mask"});

			if (element == document.body)
			{
				elt.setStyle({
					position:"absolute",

					width:document.viewport.getScrollOffsets().left + document.viewport.getDimensions().width + "px",
					height:document.viewport.getScrollOffsets().top + document.viewport.getDimensions().height + "px",

					left:element.cumulativeOffset().left + "px",
					top:element.cumulativeOffset().top + "px"
				});
			}
			else
			{
                var layout  = element.getLayout();

				elt.setStyle({
					position:"absolute",

					width:layout.get('width') + "px",
					height:layout.get('height') + "px",
					left:layout.get('left') + "px",
					top:layout.get('top') + "px"
				});

                layout = null;
			}

			elt.setOpacity(0.8);
			
			elt.update (new Element("div",{'class':"blackWaiting"}).update());

			//$(document.body).insert(elt);
			//element.getOffsetParent().insert(elt);

			//$(document.body).insert(elt);  //这样绝对不会错，为了调试查找方便，如下处理

			if (element == document.body)
			{
                $(document.body).insert(elt);
            }
            else
            {
			    element.up().insert(elt);
            }


			elt = null;

			
			element.addClassName ('mask');
		}

		return mask_id;
  },

  unmask: function(element){
		var id = element.identify();
		var prefix = 'm_';

		$(prefix + id) && $(prefix + id).del();

		element.removeClassName('mask');

		mask_index.splice(mask_index.indexOf(id), 1);
  }
}); 

/*
document.observe("dom:loaded", function()
{
	Event.observe(window, 'scroll', function ()
	{
		$$('.waiting').each(function (elt)
		{
			if (elt == $(document.body)  || elt==window || elt==document)
			{
				elt.setStyle({
					left:document.viewport.getScrollOffsets().left + "px",
					top:document.viewport.getScrollOffsets().top + "px"
				});
			}
		});
	});
})
*/

document.observe('keydown', function (evt)
{
	if (Event.KEY_ESC == evt.keyCode)
	{
		if ('input'==evt.element().tagName.toLowerCase() || 'textarea'==evt.element().tagName.toLowerCase())
		{
		}
		else
		{
			if (mask_index.size())
			{
				evt.stop();
				$(mask_index.last()).unmask();
			}
			else
			{
				evt.stop();
			}
		}
	}
	else
	{
	}

	return true;
});

//window.onresize = function()
//{
	////$$('.mask').each(function (elt)
	////{
		////$('m_' + elt.id).setStyle({
			////height:(elt || document.body).getHeight() + 'px',
			////width:(elt || document.body).getWidth() + 'px'
		////});
	////});
//}



//最新的已经过期
Element.addMethods({
  isObserved: function(element){
	//console.info(element.isObserved(), element);
    return ('_prototypeEventID' in $(element));
  },

  isObservedDescendants: function(element){
		element.descendants().invoke('isObserved');
		element.isObserved();
  }
}); 

//element.edit(),   element.del() ,  element.add()
Object.extend(Element.Methods, {

  del: function(element) {
    element = $(element);
	//element.stopObservingNested(true);
	element.stopObserving();
	element.deActions();

    element.remove();

	try
	{
		return element;
	}
	finally
	{
		element = null;
	}
  },

  edit: function(element, content) {
    element = $(element);
	//element.stopObservingNested();
	element.deActions();

	element.update(content);

    element.actions();

	try
	{
		return element;
	}
	finally
	{
		element = null;
	}
  },

  rpl: function(element, content) {
    element = $(element);
	//element.stopObservingNested();
	element.deActions();

    element.replace(content);

    element.actions();

	try
	{
		return element;
	}
	finally
	{
		element = null;
	}
  },

  add: function(element, insertions) {
    element = $(element);
	//element.stopObservingNested();
	//element.deActions();

    element.insert(insertions);
    //element.actions();

	try
	{
		return element;
	}
	finally
	{
		element = null;
	}
  },

  actions: function(element) {
    element = $(element);


	//if ('undefined' != typeof(ProtoCheck))
	//{
		//new ProtoCheck(element.id);
	//}

	if ('undefined' != typeof(mask))
	{
		box.actions(element);
	}
	
	/*
	var action = element.getElementsByClassName('func');

	for(i=0; i<action.length; i++)
	{
		action[i].observe('click', funcRel, false);
	}

	action = null;
	*/


	$$('#' + element.id +  ' .texpand').each(function(el){
		var t = new Texpand(el);
		t = null;
		el = null;
	});

	try
	{
		return element;
	}
	finally
	{
		element = null;
	}
  },


  deActions: function(element) {
    element = $(element);
	/*
	//mask
	
	var lbActions = element.getElementsByClassName('lbAction');

	for(i = 0; i < lbActions.length; i++)
	{
		lbActions[i].stopObserving('click');
	}

	lbActions = null;

	//处理tooltip
	var lbox = element.getElementsByClassName('T');

	for(i = 0; i < lbox.length; i++)
	{
		lbox[i].stopObserving('click');
	}

	lbox = null;


	lbox = element.getElementsByClassName('O');

	for(i = 0; i < lbox.length; i++)
	{
		lbox[i].stopObserving('click');
	}

	lbox = null;


	//处理掉 ProtoCheck
	//element.deActionsProtoCheck();
	*/

	//处理掉拖动
	var elts = element.getElementsByClassName('item_drop_box');
	var elts_len = elts.length;

	for(i=0; i<elts_len; i++)
	{
		//console.info(elts[i]);
		Droppables.remove(elts[i]);
		elts[i] = null;
	}

	elts = elts_len = null;


	var elts = element.getElementsByClassName('item_drag_box');
	var elts_len = elts.length;


	for(i=0; i<elts_len; i++)
	{
		var user_item_id = elts[i].readAttribute('user_item_id');


		if('undefined'!=typeof(window.drag_container[user_item_id]) && null!=window.drag_container[user_item_id])
		{
			
			//console.info(user_item_id);
			window.drag_container[user_item_id].destroy();
			window.drag_container[user_item_id] = null;
		}
		else
		{

		}
		
		elts[i] = null;
	}

	elts = elts_len = null;

	//处理掉其它注册事件

	var elts = element.getElementsByClassName('E');
	var elts_len = elts.length;

	for(i=0; i<elts_len; i++)
	{
		elts[i].stopObserving();
		//elts[i].stopObservingNested(true);
		elts[i] = null;
	}

	elts = elts_len = null;


	try
	{
		return element;
	}
	finally
	{
		element = null;
	}
  },


  deActionsProtoCheck: function(element) {
    element = $(element);

	//处理掉 ProtoCheck
	var elts = element.getElementsByClassName('pc_check');
	var elts_len = elts.length;

	for(i=0; i<elts_len; i++)
	{
		//elts[i].stopObserving();
		elts[i].stopObservingNested(true);
		elts[i] = null;
	}

	elts = element.getElementsByClassName('pc_radio');
	elts_len = elts.length;

	for(i=0; i<elts_len; i++)
	{
		elts[i].stopObservingNested(true);
		elts[i] = null;
	}

	elts = elts_len = null;

	try
	{
		return element;
	}
	finally
	{
		element = null;
	}
  }
});

function funcRel(e)
{
	e.stop();
	var elt = e.element();

	box.c(elt.readAttribute('text'), elt.readAttribute('rel'), 'n', elt.readAttribute('confirm'), elt.readAttribute('cancel'));

	elt = null;
}


function loadUrl(url, arg)
{
	if (!url)
	{
		return false;
	}

	var ajaxObj = new DAjax(url, arg);
	ajaxObj.send();

	ajaxObj = null;
	url = arg = null;

	return false;
}


function dialog(url)
{
	loadUrl(url, {logic:{type:'e'}});

	return false;
}

Element.addMethods(); 
